﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarLoanSystem.Entity;
using System.Data.SqlClient;
using System.Data;

namespace CarLoanSystem.DAL
{
    public class DeptDetailInfoDAL : DBBase
    {
        public static void Insert(DeptDetailItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"INSERT INTO [CAR_LOAN].[dbo].[DEBT_DETAIL]
                                                       ([DEBT_ID]
                                                       ,[DEBT_DETAIL]
                                                       ,[DEBT_DETAIL_DATE])
                                                 VALUES
                                                      ( @DEBT_ID
                                                       ,@DEBT_DETAIL
                                                       ,@DEBT_DETAIL_DATE)", DBBase.con);

                command.Parameters.AddWithValue("@DEBT_ID", item.DEBT_ID);
                command.Parameters.AddWithValue("@DEBT_DETAIL", item.DEBT_DETAIL);
                command.Parameters.AddWithValue("@DEBT_DETAIL_DATE", item.DEBT_DETAIL_DATE);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static DeptDetailItem LoadDeptDetail(int DEBT_DETAIL_ID)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [DEBT_DETAIL_ID]
                                                              ,[DEBT_ID]
                                                              ,[DEBT_DETAIL]
                                                              ,[DEBT_DETAIL_DATE]
                                                          FROM [CAR_LOAN].[dbo].[DEBT_DETAIL]
                                                    where [DEBT_DETAIL_ID] = @DEBT_DETAIL_ID ", DBBase.con);
                command.Parameters.AddWithValue("@DEBT_DETAIL_ID", DEBT_DETAIL_ID);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    DeptDetailItem item = new DeptDetailItem();
                    item.DEBT_DETAIL_ID = Convert.ToInt32(dt.Rows[0]["DEBT_DETAIL_ID"].ToString());
                    item.DEBT_ID = Convert.ToInt32(dt.Rows[0]["DEBT_ID"].ToString());
                    item.DEBT_DETAIL = dt.Rows[0]["DEBT_DETAIL"].ToString();
                    item.DEBT_DETAIL_DATE = Convert.ToDateTime(dt.Rows[0]["DEBT_DETAIL_DATE"].ToString());
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllDeptDetail(int DEBT_ID)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [DEBT_DETAIL_ID]
                                                              ,[DEBT_ID]
                                                              ,[DEBT_DETAIL]
                                                              ,[DEBT_DETAIL_DATE]
                                                          FROM [CAR_LOAN].[dbo].[DEBT_DETAIL]
                                                    where [DEBT_ID] = @DEBT_ID ", DBBase.con);
                command.Parameters.AddWithValue("@DEBT_ID", DEBT_ID);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                    DBBase.DisConncetDatabase();
                    return dt;
                }
                else
                {
                    DBBase.DisConncetDatabase();
                    return null;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
